# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Jonathan Curran <jonathan@curran.in>
# Contributor: Shiz <hi@shiz.me>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# secfixes:
#   6.6.2p1-r0:
#     - CVE-2020-7247
pkgname=opensmtpd
pkgver=7.5.0_p0
pkgrel=0
pkgdesc="Secure, reliable, lean, and easy-to configure SMTP server"
url="https://www.opensmtpd.org/"
# s390x: libressl
arch="all !s390x"
license="ISC"
depends="!postfix ca-certificates"
makedepends="
	bison
	db-dev
	flex-dev
	libevent-dev
	libressl-dev
	linux-pam-dev
	mandoc
	musl-fts-dev
	zlib-dev
	"
install="$pkgname.pre-install"
subpackages="$pkgname-dbg $pkgname-pam $pkgname-openrc $pkgname-doc"
source="https://www.opensmtpd.org/archives/opensmtpd-${pkgver//_/}.tar.gz
	smtpd.conf.patch
	smtpd.initd
	smtpd.confd
	aliases
	"
options="suid !check"
builddir=$srcdir/opensmtpd-${pkgver//_/}

prepare() {
	default_prepare
	cp -ar "$builddir" "$builddir-pam"
}

build() {
	_build

	cd "$builddir-pam"
	_build --with-auth-pam
}

_build() {
	CFLAGS="$CFLAGS -flto=auto" \
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc/smtpd \
		--with-libssl=/usr/include/openssl \
		--mandir=/usr/share/man \
		--libexecdir=/usr/lib \
		--with-path-mbox=/var/mail \
		--with-path-pidfile=/run \
		--with-path-socket=/run \
		--with-table-db \
		--with-user-queue=smtpq \
		--with-group-queue=smtpq \
		--with-user-smtpd=smtpd \
		--with-path-socket=/run \
		--with-libs="-lfts" \
		--with-pie \
		--with-path-CAfile=/etc/ssl/certs/ca-certificates.crt \
		--with-mantype=doc \
		"$@"
	make
}

package() {
	make DESTDIR="$pkgdir" -C "$builddir" install

	install -Dm755 "$srcdir"/smtpd.initd "$pkgdir"/etc/init.d/smtpd
	install -Dm644 "$srcdir"/smtpd.confd "$pkgdir"/etc/conf.d/smtpd
	install -Dm644 "$srcdir"/aliases "$pkgdir"/etc/smtpd/aliases

	# Create compatibility symlinks for mailq, newaliases, makemap and sendmail.
	local binary; for binary in mailq newaliases makemap sendmail; do
		ln -s /usr/sbin/smtpctl "$pkgdir"/usr/sbin/$binary
	done

	install -Dm755 "$builddir-pam"/mk/smtpd/smtpd "$pkgdir"/usr/sbin/smtpd-pam
}

pam() {
	pkgdesc="OpenSMTPD with PAM support"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/sbin/smtpd-pam
}

sha512sums="
1e4bf23e623d40732e4055a8db330b0dc69dbfbd4f3d89ef092ead3359aac4123f8a58feb61661be47d84c58e27d22c706dc308dfa245fa76f1d36ed2bb9a822  opensmtpd-7.5.0p0.tar.gz
3d9db98cc9f84afb76d2588c696573c3a508ca38301c60b7e2d9470efbf3efb004a4fb65e2d7910de4a73f06121bece86208473d8aabdecdcf6d3e3b05be123c  smtpd.conf.patch
cce0c3b014a02d46c77d4de6495cf8e7e48d17c89c27432f121060d6712ae3606a6e5d51a74cf5504e826f7dd72176297dc83c9e6623f8e3fe9a952c8d02add1  smtpd.initd
e68fca4a7e0ceda271ad61c5a6592a859789bea9ccb6417258f7a0b45d92163ed6097c208d3fdfb78bf978a6a01b6f3678e047e3ce972b2c521419d54a992e0a  smtpd.confd
51d47b34eb3d728daa45f29d6434cc75db28dfa69b6fb3ecd873121df85b296a2d2c81016d765a07778aa26a496e4b29c09a30b82678cf42596a536734b5deca  aliases
"
